package com.wxzl.community.common.base

import android.app.Application
import android.content.Context
import android.widget.Toast
import androidx.multidex.MultiDex
import com.alibaba.android.arouter.launcher.ARouter

import com.drake.brv.BindingAdapter
import com.drake.statelayout.StateConfig
import com.kongzue.dialogx.DialogX
import com.kongzue.dialogx.style.MIUIStyle
import com.mob.adsdk.AdConfig
import com.mob.adsdk.AdSdk
import com.mob.gamesdk.GameConfig
import com.mob.gamesdk.GameSdk
import com.mob.newssdk.NewsConfig
import com.mob.newssdk.NewsSdk.*
import com.mob.novelsdk.NovelConfig
import com.mob.novelsdk.NovelSdk
import com.mob.videosdk.VideoConfig
import com.mob.videosdk.VideoSdk
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.tencent.mmkv.MMKV
import com.wxzl.community.common.BR
import com.wxzl.community.common.BuildConfig
import com.wxzl.community.common.R
import com.wxzl.community.common.http.RxHttpManager
import com.wxzl.community.common.store.UserInfoStore
import com.wxzl.community.common.utils.ext.log


/**
 * Created by xiaojianjun on 2019-07-15.
 */
open class BaseApp : Application() {
    val APP_ID = "deae423077fb876242" //

    companion object {
        lateinit var instance: BaseApp
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        initOtherSdk()
        initAdSdk()
//        initAlibcSdk()

    }


    private fun initOtherSdk() {
        if (BuildConfig.DEBUG) {           // 这两行必须写在init之前，否则这些配置在init过程中将无效
            ARouter.openLog() // 打印日志
            ARouter.openDebug() // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(instance)
        MMKV.initialize(this)
        RxHttpManager.init(this)
        BindingAdapter.modelId = BR.m

        /**
         *  推荐在Application中进行全局配置缺省页, 当然同样每个页面可以单独指定缺省页.
         *  具体查看 https://github.com/liangjingkanji/StateLayout
         */
        StateConfig.apply {
            emptyLayout = R.layout.layout_view_empty
            errorLayout = R.layout.layout_load_error
            loadingLayout = R.layout.layout_loading
            setRetryIds(R.id.msg)
            onLoading {
                // 此生命周期可以拿到LoadingLayout创建的视图对象, 可以进行动画设置或点击事件.
            }
        }


        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            MaterialHeader(context)

        }
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
            ClassicsFooter(context)
        }
        DialogX.init(this)
        DialogX.globalStyle = MIUIStyle.style();
    }

    fun initAdSdk() {
        AdSdk.getInstance().init(
            applicationContext,
            AdConfig.Builder()
                .appId(APP_ID) // .userId("uid") // 未登录可不设置 userId，登录时再设置
                .multiProcess(false)
                .debug(BuildConfig.DEBUG)
                .build(),
            null
        )
        getInstance().init(
            applicationContext,
            NewsConfig.Builder()
                .appId(APP_ID)
                .debug(BuildConfig.DEBUG)
                .build(),
            null
        )
        VideoSdk.getInstance().init(
            applicationContext,
            VideoConfig.Builder()
                .appId(APP_ID)
                .debug(BuildConfig.DEBUG)
                .build(),
            null
        )
        GameSdk.getInstance().init(
            applicationContext,
            GameConfig.Builder()
                .appId(APP_ID)
                .debug(BuildConfig.DEBUG)
                .build(),
            null
        )

        NovelSdk.getInstance().init(
            applicationContext,
            NovelConfig.Builder()
                .appId(APP_ID)
                .debug(BuildConfig.DEBUG)
                .build(),
            null
        )

//        setMinSplashInterval(MIN_SPLASH_INTERVAL);

    }

//    fun  initAlibcSdk(){
//        AlibcTradeSDK.asyncInit(this, object : AlibcTradeInitCallback {
//            override fun onSuccess() {
//            // UserInfoStore.setInitAlibcSdkOK()
//            }
//
//            override fun onFailure(code: Int, msg: String) {
//                code.log()
//                msg.log()
//            }
//        })
//
//    }
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }


}